//
//  myTopicVC.swift
//  甜言蜜语
//
//  Created by yingli on 16/7/23.
//  Copyright © 2016年 wl. All rights reserved.
//

import UIKit

class myTopicVC: UIViewController, UITableViewDelegate, UITableViewDataSource {
    @IBOutlet weak var tableView: UITableView!
    var  modelList: [myTopicInfo]?{
        didSet{
            self.tableView.reloadData()
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.tableView.delegate = self
        self.tableView.dataSource = self
        
        let cellNib = UINib(nibName: "MyTopicCell", bundle: nil)
        self.tableView.registerNib(cellNib, forCellReuseIdentifier: "MyTopicCell")
        
        let cellNib1 = UINib(nibName: "MyTiopicCell2", bundle: nil)
        self.tableView.registerNib(cellNib1, forCellReuseIdentifier: "MyTiopicCell2")
        
        publicMethod.hiddleExtendCellForTableView(self.tableView)
        self.getMyTopicInfo()
    }
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.navigationBar.barTintColor = NAVIGATIONBAR_RED_COLOR
        
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
        if(cell.respondsToSelector(Selector("setSeparatorInset:"))) {
            cell.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0)
        }
        if(cell.respondsToSelector(Selector("setLayoutMargins:"))) {
            cell.layoutMargins = UIEdgeInsetsMake(0, 0, 0, 0)
        }
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        if self.modelList?.count == nil{
            return 0
        }else{
            return (self.modelList?.count)!
        }
    }
    
    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        
        let cell = tableView.dequeueReusableCellWithIdentifier("MyTopicCell") as! MyTopicCell
        let cellInfo = self.modelList![indexPath.row]
        cell.setCellContentWithCellInfo(cellInfo)
        
        return cell.cellHeight + 120

        
        
    }
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
        let model = self.modelList![indexPath.row]
        
        if model.infomationimage != ""{
        
            let cell = tableView.dequeueReusableCellWithIdentifier("MyTopicCell", forIndexPath: indexPath) as! MyTopicCell

            cell.iv.sd_setImageWithURL(NSURL(string: model.timg), placeholderImage: UIImage(named: "placeholder"))
            cell.idLbl.text = model.name
            cell.ptimeLbl.text = model.commenttime
            cell.messageLbl.text = model.message
            cell.iv2.sd_setImageWithURL(NSURL(string: model.infomationimage!), placeholderImage: UIImage(named: "placeholder"))
            cell.titleLbl.text = model.infomationtitle
                
            return cell
        }else{
             let cell = tableView.dequeueReusableCellWithIdentifier("MyTiopicCell2", forIndexPath: indexPath) as! MyTiopicCell2
            cell.iv.sd_setImageWithURL(NSURL(string: model.timg), placeholderImage: UIImage(named: "placeholder"))
            cell.idLbl.text = model.name
            cell.ptimeLbl.text = model.commenttime
            cell.messageLbl.text = model.message
             cell.titleLbl.text = model.infomationtitle
            return cell
            
        }
        
    }
    
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        tableView.deselectRowAtIndexPath(indexPath, animated: true)
        
        let sb = UIStoryboard.init(name: "NewsList", bundle: nil)
        
        let vc = sb.instantiateViewControllerWithIdentifier("DetaillNewsController") as! DetaillNewsController
        
        
    }
    
    //MARK:networking
    func getMyTopicInfo(){

        DataTool.myTopic({ (newModel) in
            guard let newDataes = newModel else {
            return
          }
            print(newDataes)
            self.modelList = newDataes
            
                    
        })
    }
}
